package com.itheima.stock.controller.v1;

import com.alibaba.excel.EasyExcel;
import com.itheima.stock.api.StockControllerAPI;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.StockInfoExcelDTO;
import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.response.PageResult;
import com.itheima.stock.response.ResponseResult;
import com.itheima.stock.service.StockService;
import com.itheima.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zby
 * @created 2024-06-23 16:33
 * @description
 */
@RestController
public class StockController implements StockControllerAPI {

    private final StockService stockService;

    public StockController(StockService stockService) {
        this.stockService = stockService;
    }

    @Override
    public ResponseResult<StockUpDownVO<InfoVO>> stockUpDown() {
        return ResponseResult.ok(stockService.stockUpDown());
    }

    @Override
    public ResponseResult<TradeAmtVO<OptionVO>> tradeAmt() {
        return ResponseResult.ok(stockService.tradeAmt());
    }

    @Override
    public void export(int page, int pageSize, HttpServletResponse response) throws IOException {
        // 1. 设定响应数据类型 excel文件
        response.setContentType("application/vnd.ms-excel");
        // 2. 指定响应编码
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(Constants.EXCEL_FILE_PREFIX + page + Constants.EXCEL_FILE_SUFFIX, StandardCharsets.UTF_8);
        // 3. 指定附件下载
        response.setHeader("Content-disposition", "attachment;filename="+fileName);

        // 4.1 准备要写的数据 查询数据库
        AtomicInteger counter = new AtomicInteger(0);
        EasyExcel.write(response.getOutputStream(), StockInfoExcelDTO.class)
                .sheet(Constants.EXCEL_FILE_PREFIX+page)
                .doWrite(stockService.more(page, pageSize).getRows().stream().map((vo) -> {
                    StockInfoExcelDTO dto = new StockInfoExcelDTO();
                    BeanUtils.copyProperties(vo, dto);
                    dto.setNum(counter.incrementAndGet());
                    return dto;
                }).toList());
    }

    @Override
    public ResponseResult<UpDownVO<OptionVO>> upDown() {
        return ResponseResult.ok(stockService.upDown());
    }

    @Override
    public ResponseResult<PageResult<MoreVO>> more(Integer page, Integer pageSize) {
        return ResponseResult.ok(stockService.more(page, pageSize));
    }

    @Override
    public ResponseResult<List<IncreaseVO>> stockIncrease() {
        return ResponseResult.ok(stockService.stockIncrease());
    }

    @Override
    public ResponseResult<List<SectorAllVO>> sectorAll() {
        return ResponseResult.ok(stockService.sectorAll());
    }

    @Override
    public ResponseResult<List<MarketIndexVO>> innerIndexAll() {
        return ResponseResult.ok(stockService.innerIndexAll());
    }

    @Override
    public ResponseResult<List<StockBusiness>> getStockBusiness() {
        List<StockBusiness> stockBusiness = stockService.getStockBusiness();
        return ResponseResult.ok(stockBusiness);
    }
}
